/*
 * @(#)MtmCalendarUserUserActor.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.calendar.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.dcivision.calendar.bean.MtmCalendarUserUserActor;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractSearchForm;

/**
  MtmCalendarUserUserActorDAObject.java

  This class is the data access bean for table "MTM_CALENDAR_USER_USER_ACTOR".

  @author      Tony Chen
  @company     DCIVision Limited
  @creation date   05/12/2003
  @version     $Revision: 1.7.26.1 $
*/

public class MtmCalendarUserUserActorDAObject extends AbstractDAObject {

  public static final String REVISION = "$Revision: 1.7.26.1 $";

  public static final String TABLE_NAME = "MTM_CALENDAR_USER_USER_ACTOR";

  public MtmCalendarUserUserActorDAObject(SessionContainer sessionContainer, Connection dbConn) {
    super(sessionContainer, dbConn);
  }

  
  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.vecDBColumn.add("ID");
    this.vecDBColumn.add("USER_RECORD_ID");
    this.vecDBColumn.add("ACTOR_TYPE");
    this.vecDBColumn.add("ACTOR_ID");
    this.vecDBColumn.add("RECORD_STATUS");
    this.vecDBColumn.add("UPDATE_COUNT");
    this.vecDBColumn.add("CREATOR_ID");
    this.vecDBColumn.add("CREATE_DATE");
    this.vecDBColumn.add("UPDATER_ID");
    this.vecDBColumn.add("UPDATE_DATE");
  }

  
  protected synchronized AbstractBaseObject getByID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.USER_RECORD_ID, A.ACTOR_TYPE, A.ACTOR_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_CALENDAR_USER_USER_ACTOR A ");
        sqlStat.append("WHERE  A.ID = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString());
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        if (rs.next()) {
          MtmCalendarUserUserActor tmpMtmCalendarUserUserActor = new MtmCalendarUserUserActor();
          tmpMtmCalendarUserUserActor.setID(getResultSetInteger(rs, "ID"));
          tmpMtmCalendarUserUserActor.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
          tmpMtmCalendarUserUserActor.setActorType(getResultSetString(rs, "ACTOR_TYPE"));
          tmpMtmCalendarUserUserActor.setActorID(getResultSetInteger(rs, "ACTOR_ID"));
          tmpMtmCalendarUserUserActor.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmCalendarUserUserActor.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmCalendarUserUserActor.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmCalendarUserUserActor.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmCalendarUserUserActor.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmCalendarUserUserActor.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmCalendarUserUserActor.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmCalendarUserUserActor.getCreatorID()));
          tmpMtmCalendarUserUserActor.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmCalendarUserUserActor.getUpdaterID()));
          return(tmpMtmCalendarUserUserActor);
        } else {
          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized List getList(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.USER_RECORD_ID, A.ACTOR_TYPE, A.ACTOR_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_CALENDAR_USER_USER_ACTOR A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");

        // Form the WHERE clause for filtering.
        if (searchForm.isSearchable()) {
          String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }

        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }

        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString());
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStatCnt, 2, searchKeyword);
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStat, 2, searchKeyword);
        }
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt<pageSize) {
          MtmCalendarUserUserActor tmpMtmCalendarUserUserActor = new MtmCalendarUserUserActor();
          tmpMtmCalendarUserUserActor.setID(getResultSetInteger(rs, "ID"));
          tmpMtmCalendarUserUserActor.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
          tmpMtmCalendarUserUserActor.setActorType(getResultSetString(rs, "ACTOR_TYPE"));
          tmpMtmCalendarUserUserActor.setActorID(getResultSetInteger(rs, "ACTOR_ID"));
          tmpMtmCalendarUserUserActor.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmCalendarUserUserActor.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmCalendarUserUserActor.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmCalendarUserUserActor.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmCalendarUserUserActor.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmCalendarUserUserActor.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmCalendarUserUserActor.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmCalendarUserUserActor.getCreatorID()));
          tmpMtmCalendarUserUserActor.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmCalendarUserUserActor.getUpdaterID()));
          tmpMtmCalendarUserUserActor.setRecordCount(totalNumOfRecord);
          tmpMtmCalendarUserUserActor.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpMtmCalendarUserUserActor);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
      }
    }
  }

  
  protected synchronized List getList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.USER_RECORD_ID, A.ACTOR_TYPE, A.ACTOR_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_CALENDAR_USER_USER_ACTOR A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString());
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmCalendarUserUserActor tmpMtmCalendarUserUserActor = new MtmCalendarUserUserActor();
          tmpMtmCalendarUserUserActor.setID(getResultSetInteger(rs, "ID"));
          tmpMtmCalendarUserUserActor.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
          tmpMtmCalendarUserUserActor.setActorType(getResultSetString(rs, "ACTOR_TYPE"));
          tmpMtmCalendarUserUserActor.setActorID(getResultSetInteger(rs, "ACTOR_ID"));
          tmpMtmCalendarUserUserActor.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmCalendarUserUserActor.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmCalendarUserUserActor.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmCalendarUserUserActor.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmCalendarUserUserActor.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmCalendarUserUserActor.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmCalendarUserUserActor.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmCalendarUserUserActor.getCreatorID()));
          tmpMtmCalendarUserUserActor.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmCalendarUserUserActor.getUpdaterID()));
          result.add(tmpMtmCalendarUserUserActor);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected void validateInsert(AbstractBaseObject obj) throws ApplicationException {
  }

  
  protected void validateUpdate(AbstractBaseObject obj) throws ApplicationException {
  }

  
  protected void validateDelete(AbstractBaseObject obj) throws ApplicationException {
  }

  
  protected synchronized AbstractBaseObject insert(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmCalendarUserUserActor tmpMtmCalendarUserUserActor = (MtmCalendarUserUserActor)((MtmCalendarUserUserActor)obj).clone();

    synchronized(dbConn) {
      try {
        Integer nextID = getNextPrimaryID();
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("INSERT ");
        sqlStat.append("INTO   MTM_CALENDAR_USER_USER_ACTOR(ID, USER_RECORD_ID, ACTOR_TYPE, ACTOR_ID, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE) ");
        sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
        preStat = dbConn.prepareStatement(sqlStat.toString());
        setPrepareStatement(preStat, 1, nextID);
        setPrepareStatement(preStat, 2, tmpMtmCalendarUserUserActor.getUserRecordID());
        setPrepareStatement(preStat, 3, tmpMtmCalendarUserUserActor.getActorType());
        setPrepareStatement(preStat, 4, tmpMtmCalendarUserUserActor.getActorID());
        setPrepareStatement(preStat, 5, GlobalConstant.RECORD_STATUS_ACTIVE);
        setPrepareStatement(preStat, 6, new Integer(0));
        setPrepareStatement(preStat, 7, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 8, currTime);
        setPrepareStatement(preStat, 9, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 10, currTime);
        preStat.executeUpdate();
        tmpMtmCalendarUserUserActor.setID(nextID);
        tmpMtmCalendarUserUserActor.setCreatorID(sessionContainer.getUserRecordID());
        tmpMtmCalendarUserUserActor.setCreateDate(currTime);
        tmpMtmCalendarUserUserActor.setUpdaterID(sessionContainer.getUserRecordID());
        tmpMtmCalendarUserUserActor.setUpdateDate(currTime);
        tmpMtmCalendarUserUserActor.setUpdateCount(new Integer(0));
        tmpMtmCalendarUserUserActor.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmCalendarUserUserActor.getCreatorID()));
        tmpMtmCalendarUserUserActor.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmCalendarUserUserActor.getUpdaterID()));
        return(tmpMtmCalendarUserUserActor);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized AbstractBaseObject update(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmCalendarUserUserActor tmpMtmCalendarUserUserActor = (MtmCalendarUserUserActor)((MtmCalendarUserUserActor)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE MTM_CALENDAR_USER_USER_ACTOR ");
        sqlStat.append("SET  USER_RECORD_ID=?, ACTOR_TYPE=?, ACTOR_ID=?, UPDATE_COUNT=?, UPDATER_ID=?, UPDATE_DATE=? ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString());
        setPrepareStatement(preStat, 1, tmpMtmCalendarUserUserActor.getUserRecordID());
        setPrepareStatement(preStat, 2, tmpMtmCalendarUserUserActor.getActorType());
        setPrepareStatement(preStat, 3, tmpMtmCalendarUserUserActor.getActorID());
        setPrepareStatement(preStat, 4, new Integer(tmpMtmCalendarUserUserActor.getUpdateCount().intValue() + 1));
        setPrepareStatement(preStat, 5, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 6, currTime);
        setPrepareStatement(preStat, 7, tmpMtmCalendarUserUserActor.getID());
        setPrepareStatement(preStat, 8, tmpMtmCalendarUserUserActor.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          tmpMtmCalendarUserUserActor.setUpdaterID(sessionContainer.getUserRecordID());
          tmpMtmCalendarUserUserActor.setUpdateDate(currTime);
          tmpMtmCalendarUserUserActor.setUpdateCount(new Integer(tmpMtmCalendarUserUserActor.getUpdateCount().intValue() + 1));
          tmpMtmCalendarUserUserActor.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmCalendarUserUserActor.getCreatorID()));
          tmpMtmCalendarUserUserActor.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmCalendarUserUserActor.getUpdaterID()));
          return(tmpMtmCalendarUserUserActor);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized AbstractBaseObject delete(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmCalendarUserUserActor tmpMtmCalendarUserUserActor = (MtmCalendarUserUserActor)((MtmCalendarUserUserActor)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   MTM_CALENDAR_USER_USER_ACTOR ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString());
        setPrepareStatement(preStat, 1, tmpMtmCalendarUserUserActor.getID());
        setPrepareStatement(preStat, 2, tmpMtmCalendarUserUserActor.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          return(tmpMtmCalendarUserUserActor);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized void auditTrail(String opMode, AbstractBaseObject obj) throws ApplicationException {
    Vector oldValues = new Vector();
    Vector newValues = new Vector();
    MtmCalendarUserUserActor tmpMtmCalendarUserUserActor = (MtmCalendarUserUserActor)this.oldValue;

    if (tmpMtmCalendarUserUserActor != null) {
      oldValues.add(toAuditTrailValue(tmpMtmCalendarUserUserActor.getID()));
      oldValues.add(toAuditTrailValue(tmpMtmCalendarUserUserActor.getUserRecordID()));
      oldValues.add(toAuditTrailValue(tmpMtmCalendarUserUserActor.getActorType()));
      oldValues.add(toAuditTrailValue(tmpMtmCalendarUserUserActor.getActorID()));
      oldValues.add(toAuditTrailValue(tmpMtmCalendarUserUserActor.getRecordStatus()));
      oldValues.add(toAuditTrailValue(tmpMtmCalendarUserUserActor.getUpdateCount()));
      oldValues.add(toAuditTrailValue(tmpMtmCalendarUserUserActor.getCreatorID()));
      oldValues.add(toAuditTrailValue(tmpMtmCalendarUserUserActor.getCreateDate()));
      oldValues.add(toAuditTrailValue(tmpMtmCalendarUserUserActor.getUpdaterID()));
      oldValues.add(toAuditTrailValue(tmpMtmCalendarUserUserActor.getUpdateDate()));
    }

    tmpMtmCalendarUserUserActor = (MtmCalendarUserUserActor)obj;
    if (tmpMtmCalendarUserUserActor != null) {
      newValues.add(toAuditTrailValue(tmpMtmCalendarUserUserActor.getID()));
      newValues.add(toAuditTrailValue(tmpMtmCalendarUserUserActor.getUserRecordID()));
      newValues.add(toAuditTrailValue(tmpMtmCalendarUserUserActor.getActorType()));
      newValues.add(toAuditTrailValue(tmpMtmCalendarUserUserActor.getActorID()));
      newValues.add(toAuditTrailValue(tmpMtmCalendarUserUserActor.getRecordStatus()));
      newValues.add(toAuditTrailValue(tmpMtmCalendarUserUserActor.getUpdateCount()));
      newValues.add(toAuditTrailValue(tmpMtmCalendarUserUserActor.getCreatorID()));
      newValues.add(toAuditTrailValue(tmpMtmCalendarUserUserActor.getCreateDate()));
      newValues.add(toAuditTrailValue(tmpMtmCalendarUserUserActor.getUpdaterID()));
      newValues.add(toAuditTrailValue(tmpMtmCalendarUserUserActor.getUpdateDate()));
    }

    auditTrailBase(opMode, oldValues, newValues);
  }

  /***********************************************************************
   * DON'T Modify the codes above unless you know what you are doing!!!  *
   * Put your own functions beblow.                                      *
   * For FINDER methods, the function name should be in the notation:    *
   *   public Object getObjectBy<Criteria>()                             *
   *   - e.g. public Object getObjectByCode()                            *
   *   public List getListBy<Criteria>()                                 *
   *   - e.g. public List getListByUserID()                              *
   * For OPERATION methods, the function name should be in the notation: *
   *   public void <Operation>ObjectBy<Criteria>()                       *
   *   - e.g. public void deleteObjectByCode()                           *
   *   public void <Operation>ListBy<Criteria>()                         *
   *   - e.g. public void deleteListByUserID()                           *
   ***********************************************************************/

  public synchronized List getListBySharerIDActorType(Integer sharerID, String[] actorType) throws ApplicationException {
        PreparedStatement preStat = null;
        ResultSet rs = null;
        StringBuffer sqlStat = new StringBuffer();
        List result = new ArrayList();

        synchronized (dbConn) {
            try{
                sqlStat.append("SELECT ID, USER_RECORD_ID, ACTOR_TYPE, ACTOR_ID, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE ");
                sqlStat.append("FROM MTM_CALENDAR_USER_USER_ACTOR ");
                sqlStat.append("WHERE USER_RECORD_ID = ? ");
                if (actorType!=null){
                    sqlStat.append("AND (");
                    for (int i=0; i<actorType.length; i++) {
                        sqlStat.append("ACTOR_TYPE = ? OR ");
                    }
                    sqlStat.delete(sqlStat.length()-3, sqlStat.length());
                    sqlStat.append(") ");
                }
                sqlStat.append(" ORDER BY ACTOR_TYPE, ID");
                preStat = dbConn.prepareStatement(sqlStat.toString());

                this.setPrepareStatement(preStat, 1, sharerID);
                if (actorType!=null) {
                    for (int i=0; i<actorType.length; i++) {
                        this.setPrepareStatement(preStat, i+2, actorType[i]);
                    }
                }

                rs = preStat.executeQuery();
                while (rs.next()){
                    MtmCalendarUserUserActor tmpMtmCalendarUserUserActor = new MtmCalendarUserUserActor();
                    tmpMtmCalendarUserUserActor.setID(getResultSetInteger(rs, "ID"));
                    tmpMtmCalendarUserUserActor.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
                    tmpMtmCalendarUserUserActor.setActorType(getResultSetString(rs, "ACTOR_TYPE"));
                    tmpMtmCalendarUserUserActor.setActorID(getResultSetInteger(rs, "ACTOR_ID"));
                    tmpMtmCalendarUserUserActor.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
                    tmpMtmCalendarUserUserActor.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
                    tmpMtmCalendarUserUserActor.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
                    tmpMtmCalendarUserUserActor.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
                    tmpMtmCalendarUserUserActor.setCreatorName(sessionContainer == null ? "" : sessionContainer.getUserRecord().getCreatorName());
                    tmpMtmCalendarUserUserActor.setUpdaterName(sessionContainer == null ? "" : sessionContainer.getUserRecord().getUpdaterName());
                    result.add(tmpMtmCalendarUserUserActor);
                }
                return result;
            } catch (ApplicationException appEx) {
                throw appEx;
            } catch (SQLException sqle) {
                log.error(sqle, sqle);
                throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle);
            } catch (Exception e) {
                log.error(e, e);
                throw new ApplicationException(ErrorConstant.COMMON_FATAL_ERROR, e);
            } finally {
                try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
            }
        }
    }

    public synchronized void deleteListBySharerIDActorType(Integer sharerID, String[] actorType) throws ApplicationException {
        PreparedStatement preStat = null;
        ResultSet rs = null;
        StringBuffer sqlStat = new StringBuffer();

        synchronized(dbConn) {
            try{
                int updateCnt = 0;
                sqlStat.append("DELETE ");
                sqlStat.append("FROM   MTM_CALENDAR_USER_USER_ACTOR ");
                sqlStat.append("WHERE  USER_RECORD_ID=? ");
                if (actorType!=null) {
                    sqlStat.append("AND (");
                    for (int i=0; i<actorType.length; i++) {
                        sqlStat.append("ACTOR_TYPE = ? OR ");
                    }
                    sqlStat.delete(sqlStat.length()-3, sqlStat.length());
                    sqlStat.append(")");
                }
                preStat = dbConn.prepareStatement(sqlStat.toString());
                setPrepareStatement(preStat, 1, sharerID);
                if (actorType!=null) {
                    for (int i=0; i<actorType.length; i++) {
                        setPrepareStatement(preStat, i+2, actorType[i]);
                    }
                }
                updateCnt = preStat.executeUpdate();
                if (updateCnt == 0) {
                    throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
                }
            } catch (ApplicationException appEx) {
                throw appEx;
            } catch (SQLException sqle) {
                log.error(sqle, sqle);
                throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle);
            } catch (Exception e) {
                log.error(e, e);
                throw new ApplicationException(ErrorConstant.COMMON_FATAL_ERROR, e);
            } finally {
                try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
            }
        }
    }

    public synchronized List getUserIDListByShareIDGroupActorType(Integer sharerID) throws ApplicationException {
        PreparedStatement preStat = null;
        ResultSet rs = null;
        StringBuffer sqlStat = new StringBuffer();
        List result = new ArrayList();

        synchronized (dbConn) {
            try{
                sqlStat.append("SELECT DISTINCT UR.ID ");
                sqlStat.append("FROM MTM_USER_RECORD_USER_GROUP MURUG, MTM_CALENDAR_USER_USER_ACTOR MCUUA, USER_RECORD UR ");
                sqlStat.append("WHERE MURUG.USER_GROUP_ID=MCUUA.ACTOR_ID AND UR.ID=MURUG.USER_RECORD_ID AND MCUUA.ACTOR_TYPE = ? ");
                sqlStat.append("AND MCUUA.USER_RECORD_ID = ? ");

                preStat = dbConn.prepareStatement(sqlStat.toString());
                setPrepareStatement(preStat, 1, MtmCalendarUserUserActor.ACTOR_TYPE_GROUP);
                setPrepareStatement(preStat, 2, sharerID);

                rs = preStat.executeQuery();
                while (rs.next()){
                    result.add(this.getResultSetInteger(rs, "ID"));
                }
                return result;
            } catch (ApplicationException appEx) {
                throw appEx;
            } catch (SQLException sqle) {
                log.error(sqle, sqle);
                throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle);
            } catch (Exception e) {
                log.error(e, e);
                throw new ApplicationException(ErrorConstant.COMMON_FATAL_ERROR, e);
            } finally {
                try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
            }
        }
    }

    public synchronized List getUserIDListByShareIDRoleActorType(Integer sharerID) throws ApplicationException {
        PreparedStatement preStat = null;
        ResultSet rs = null;
        StringBuffer sqlStat = new StringBuffer();
        List result = new ArrayList();

        synchronized (dbConn) {
            try{
                sqlStat.append("SELECT DISTINCT UR.ID ");
                sqlStat.append("FROM MTM_USER_RECORD_USER_ROLE MURUR, MTM_CALENDAR_USER_USER_ACTOR MCUUA, USER_RECORD UR ");
                sqlStat.append("WHERE MURUR.USER_ROLE_ID=MCUUA.ACTOR_ID AND MURUR.USER_RECORD_ID=UR.ID AND MCUUA.ACTOR_TYPE = ? ");
                sqlStat.append("AND MCUUA.USER_RECORD_ID = ? ");

                preStat = dbConn.prepareStatement(sqlStat.toString());
                setPrepareStatement(preStat, 1, MtmCalendarUserUserActor.ACTOR_TYPE_ROLE);
                setPrepareStatement(preStat, 2, sharerID);

                rs = preStat.executeQuery();
                while (rs.next()){
                    result.add(this.getResultSetInteger(rs, "ID"));
                }
                return result;
            } catch (ApplicationException appEx) {
                throw appEx;
            } catch (SQLException sqle) {
                log.error(sqle, sqle);
                throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle);
            } catch (Exception e) {
                log.error(e, e);
                throw new ApplicationException(ErrorConstant.COMMON_FATAL_ERROR, e);
            } finally {
                try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
            }
        }
    }

    public synchronized List getUserIDListByShareIDUserActorType(Integer sharerID) throws ApplicationException {
        PreparedStatement preStat = null;
        ResultSet rs = null;
        StringBuffer sqlStat = new StringBuffer();
        List result = new ArrayList();

        synchronized (dbConn) {
            try{
                sqlStat.append("SELECT DISTINCT UR.ID "); //USER_LINK_ID ");
                sqlStat.append("FROM MTM_CALENDAR_USER_USER_ACTOR MCUUA, USER_RECORD UR ");
                sqlStat.append("WHERE MCUUA.ACTOR_ID=UR.ID AND MCUUA.ACTOR_TYPE = ? ");
                sqlStat.append("AND MCUUA.USER_RECORD_ID = ? ");
//                log.debug("SQL: \n"+sqlStat.toString());
//                log.debug("TYPE: "+ MtmCalendarUserUserActor.ACTOR_TYPE_USER);
//                log.debug("ID: "+sharerID);
                preStat = dbConn.prepareStatement(sqlStat.toString());
                setPrepareStatement(preStat, 1, MtmCalendarUserUserActor.ACTOR_TYPE_USER);
                setPrepareStatement(preStat, 2, sharerID);

                rs = preStat.executeQuery();
                while (rs.next()){
                    result.add(this.getResultSetInteger(rs, "ID"));
                }
                return result;
            } catch (ApplicationException appEx) {
                throw appEx;
            } catch (SQLException sqle) {
                log.error(sqle, sqle);
                throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle);
            } catch (Exception e) {
                log.error(e, e);
                throw new ApplicationException(ErrorConstant.COMMON_FATAL_ERROR, e);
            } finally {
                try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
            }
        }
    }

    public synchronized List getActorIDListByShareIDUserActorType(Integer sharerID, String actorType) throws ApplicationException {
          PreparedStatement preStat = null;
          ResultSet rs = null;
          StringBuffer sqlStat = new StringBuffer();
          List result = new ArrayList();

          synchronized (dbConn) {
              try{
                  sqlStat.append("SELECT DISTINCT ACTOR_ID ");
                  sqlStat.append("FROM MTM_CALENDAR_USER_USER_ACTOR ");
                  sqlStat.append("WHERE ACTOR_TYPE = ? ");
                  sqlStat.append("AND USER_RECORD_ID = ? ");
                  preStat = dbConn.prepareStatement(sqlStat.toString());
                  setPrepareStatement(preStat, 1, actorType);
                  setPrepareStatement(preStat, 2, sharerID);

                  rs = preStat.executeQuery();
                  while (rs.next()){
                      result.add(this.getResultSetInteger(rs, "ACTOR_ID"));
                  }
                  return result;
              } catch (ApplicationException appEx) {
                  throw appEx;
              } catch (SQLException sqle) {
                  log.error(sqle, sqle);
                  throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle);
              } catch (Exception e) {
                  log.error(e, e);
                  throw new ApplicationException(ErrorConstant.COMMON_FATAL_ERROR, e);
              } finally {
                  try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
              }
          }
    }
    /*
    public synchronized List getByUserIDsIDGroupsRoles(Integer sharerID, String actorType, List actorIDList) throws ApplicationException {
     PreparedStatement preStat = null;
     ResultSet rs = null;
     StringBuffer sqlStat = new StringBuffer();
     List resultList = new ArrayList();
     int x = 1;

     synchronized(dbConn) {
         try {
             sqlStat.append("SELECT DISTINCT ACTOR_ID ");
             sqlStat.append("FROM   MTM_CALENDAR_USER_USER_ACTOR MCUUA, USER_RECORD UR");
             if (MtmCalendarUserUserActor.ACTOR_TYPE_GROUP.equals(actorType)) {
               sqlStat.append(", MTM_USER_RECORD_USER_GROUP MURUG ");
             } else if (MtmCalendarUserUserActor.ACTOR_TYPE_ROLE.equals(actorType)) {
               sqlStat.append(", MTM_USER_RECORD_USER_ROLE MURUR ");
             }
             sqlStat.append("WHERE MCUUA. ");
             if (userID!=null) {
               for (int i=0; i<userID.size(); i++){
                 sqlStat.append(" (ACTOR_TYPE = 'U') AND (ACTOR_ID = ?) AND (USER_RECORD_ID=?)  ");
               }
             }
             if (userGroupList!=null) {
               for (int i=0; i<userGroupList.size(); i++){
                 sqlStat.append("OR (ACTOR_TYPE = 'G') AND (ACTOR_ID = ?) AND (USER_RECORD_ID=?) ");
               }
             }
             if (userRoleList!=null) {
               for (int i=0; i<userRoleList.size(); i++){
                 sqlStat.append("OR (ACTOR_TYPE = 'R') AND (ACTOR_ID = ?) AND (USER_RECORD_ID=?) ");
               }
             }

             preStat = dbConn.prepareStatement(sqlStat.toString());
             //log.debug(sqlStat.toString());
             this.setPrepareStatement(preStat, 1, userID);

             if (userGroupList.size()!=0) {
               for (int i=0; i<userGroupList.size(); i++){
                 x++;
                 MtmUserRecordUserGroup userGroup = (MtmUserRecordUserGroup) userGroupList.get(i);
                 this.setPrepareStatement(preStat, x, userGroup.getUserGroupID());
               }
             }
             if (userRoleList.size()!=0) {
               for (int i=0; i<userRoleList.size(); i++){
                 x++;
                 MtmUserRecordUserRole userRole = (MtmUserRecordUserRole) userRoleList.get(i);
                 this.setPrepareStatement(preStat, x, userRole.getUserRoleID());
               }
             }
             rs = preStat.executeQuery();
             while (rs.next()) {
                 /*MtmUpdateAlertRecipient tmpMtmUpdateAlertRecipient = new MtmUpdateAlertRecipient();
                 tmpMtmUpdateAlertRecipient.setUpdateAlertID(getResultSetInteger(rs, "UPDATE_ALERT_ID"));
                 tmpMtmUpdateAlertRecipient.setRecipientType(getResultSetString(rs, "RECIPIENT_TYPE"));
                 tmpMtmUpdateAlertRecipient.setRecipientID(getResultSetInteger(rs, "RECIPIENT_ID"));
                 tmpMtmUpdateAlertRecipient.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
                 tmpMtmUpdateAlertRecipient.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
                 tmpMtmUpdateAlertRecipient.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
                 tmpMtmUpdateAlertRecipient.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
                 tmpMtmUpdateAlertRecipient.setCreatorName(sessionCtx.getUserName(tmpMtmUpdateAlertRecipient.getCreatorID()));
                 tmpMtmUpdateAlertRecipient.setUpdaterName(sessionCtx.getUserName(tmpMtmUpdateAlertRecipient.getUpdaterID()));

                 Integer updateAlertID = getResultSetInteger(rs, "UPDATE_ALERT_ID");
                 resultList.add((Integer) updateAlertID);
             }
         } catch (ApplicationException appEx) {
             throw appEx;
         } catch (SQLException sqle) {
             log.error(sqle, sqle);
             throw new ApplicationException("common.E00001", sqle);
         } catch (Exception e) {
             log.error(e, e);
             throw new ApplicationException("common.E00009", e);
         } finally {
             try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
             try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
         }
     }

     return resultList;
     }
 }
    */


}
